<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-norma layui-btn-xs" lay-event="add">添加</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var form = layui.form;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#munu-table',
            url: '/api/menu?method=getMenuList',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'title', minWidth: 200, title: '菜单名称'},
                {field: 'icon', title: '图标'},
                {field: 'href', title: '菜单url'},
                {field: 'target', width: 100, align: 'center', title: '打开方式'},
                {field: 'create_at', width: 200, align: 'center', title: '创建时间'},
                {
                    field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                        if (d.isMenu == 1) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                        if (d.parentId == -1) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {field: 'status', width: 120, title: '状态', templet: '#statusTpl'},
                {templet: '#auth-state', width: 180, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.msg('删除' + data.id);
            } else if (layEvent === 'edit') {
                layer.msg('修改' + data.id);
            }
        });
        // 监听开关事件
        form.on('switch(status)', function (data) {
            var myChecked = this.checked;
            var status = this.checked ? '1' : '0';
            //获取所需属性值
            var menu_id = data.elem.attributes['menu_id'].nodeValue;
            layer.confirm('您确定要更改吗？', {
                btn: ['确定', '取消']
                , yes: function (index) {
                    layer.close(index);
                    $.ajax({
                        url: "/api/menu?method=updateMenuStatus&menuId=" + menu_id + "&status=" + status,
                        type: "get",
                        success: function (data) {
                            console.log(data);
                            if (data.code === 200) {
                                data.elem.checked = myChecked;
                                form.render();
                                //按钮【按钮一】的回调
                            } else {
                                layer.msg('删除失败', function () {
                                    //window.location = '../userlist.html';
                                });
                            }
                        }
                    });
                }
                , btn2: function (index) {
                    //按钮【按钮二】的回调
                    data.elem.checked = !myChecked;
                    form.render();
                    layer.close(index);
                    //return false; //开启该代码可禁止点击该按钮关闭
                }
            });
        });
    });
</script>
<script type="text/html" id="statusTpl">
    {{#  if(d.status==1){ }}
    <input type="checkbox" name="status" lay-skin="switch" checked lay-text="启用|停用" value={{ d.status }}
           lay-filter="status" menu_id="{{d.id}}">
    {{#  } else { }}
    <input type="checkbox" name="status" lay-skin="switch" lay-text="启用|停用" value={{ d.status}}
           lay-filter="status" menu_id="{{d.id}}">
    {{#  } }}
</script>
</body>

</html>